package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.message.bean.ContactGroup;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.GlideHelper;

import java.util.HashSet;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/7/3
 **/
public class ChatRemoveMemberAdapter extends BaseNormalListAdapter<ContactGroup.Member, ChatRemoveMemberAdapter.ViewHolder> {

    private HashSet<Integer> selectedUserIds = new HashSet<>();
    private OnSelectListener l;

    public ChatRemoveMemberAdapter(Context context, OnSelectListener l) {
        super(context);
        this.l = l;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_message_chat_remove_member, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        ContactGroup.Member data = getData(position);

        int userId = data.getId();

        holder.tv.setText(data.getUsername());
        GlideHelper.setCircleImage(mContext, data.getUser_img(), holder.iv);
        holder.cb.setChecked(selectedUserIds.contains(userId));
        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.cb.isChecked()) {
                    selectedUserIds.add(userId);
                } else {
                    selectedUserIds.remove(userId);
                }
                if (l != null) {
                    l.onSelect(selectedUserIds.size() > 0);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.cb.performClick();
            }
        });
    }

    public HashSet<Integer> getSelectedUserIds() {
        return selectedUserIds;
    }

    public interface OnSelectListener {
        void onSelect(boolean b);
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        TextView tv;
        ImageView iv;
        CheckBox cb;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
            iv = itemView.findViewById(R.id.iv);
            cb = itemView.findViewById(R.id.cb);
        }
    }
}


























